$(function () {
    var channel = getQueryString("channel");
    getLikeData(channel);
    getShelfData();
    $(".back-home").click(function () {
        location.href = "../index.html?channel="+getQueryString("channel");
    });
    $(".tab-wrapper .tab").click(function (e) {
        if($(this).attr("data-target") == "myshelf"){
            $("body").css("background","rgb(252,252,252)");
            $(".shelf-content").show();
            $(".track-content").hide();
            $(".btn-wrapper").show();
            console.log("加载书架内容");
        }else{
            $("body").css("background","rgb(245,245,245)");
            $(".track-content").show();
            $(".shelf-content").hide();
            $(".btn-wrapper").hide();
            // 清空选择框
            $(".manage").show();
            $(".cancel").hide();
            $(".select-all-wrapper").hide();
            $(".select-box").hide().removeClass("selected");
            $(".select-all").show();
            $(".not-select-all").hide();
            getTrackData(channel);
            console.log("加载阅读足迹内容");
        }
        $(this).addClass("active").siblings("div").removeClass("active");
    });
    $(".go-to-home").click(function () {
        location.href = "../index.html?channel="+channel;
    });
    // 当没有点击管理键时，无法选中书架的书，并且点击书本跳转到小说详情页
    var canSelect = false;
    $(".manage").click(function () {
        canSelect = true;
        $(this).hide();
        $(".cancel").show();
        $(".select-all-wrapper").show();
        $(".select-box").show();
    });
    $(".cancel").click(function () {
        canSelect = false;
        $(this).hide();
        $(".manage").show();
        $(".select-all-wrapper").hide();
        $(".select-box").hide().removeClass("selected");
        $(".select-all").show();
        $(".not-select-all").hide();
    });
    $(".shelf-list").on("click","li",function () {
        console.log(canSelect,111);
        if(canSelect){
            $(this).children(".select-box").toggleClass("selected");
            var totalCount = $(".shelf-list li").length;
            var selectedCount = $(".selected").length;
            // 判断是否全部选中
            if (selectedCount == totalCount){
                $(".select-all").hide();
                $(".not-select-all").show();
            }else {
                $(".select-all").show();
                $(".not-select-all").hide();
            }
        }else{
            var id = $(this).children("i").attr("data-id")
            location.href = "./detail.html?id="+id+"&channel="+channel;
        }
    });
    $(".select-all").click(function () {
        $(".select-box").addClass("selected");
        $(this).hide();
        $(".not-select-all").show();
    });
    $(".not-select-all").click(function () {
        $(".select-box").removeClass("selected");
        $(this).hide();
        $(".select-all").show();
    });
    $(".del").click(function () {
        canSelect = false;
        var selectedLi = $(".selected");
        var ids = [];
        for(var i = 0;i < selectedLi.length;i ++){
            ids.push($(selectedLi[i]).attr("data-index"));
        }
        deleteShelf(ids.join(","));
        console.log(ids.join(","));
    });
    $(".close-wrapper").click(function () {
        $(".like-mask-wrapper").hide();
    });
});

function getLikeData(channel) {
    $.ajax({
        type:'get',
        url:BASE_URl + "topic/gene?channel="+channel+"&size=3&format=normal&push=true",
        success:function (data) {
            if(data.success){
                DrawLike(data.data,channel);
            }else{
                $(".like-mask-wrapper").hide();
            }
        }
    });
}

function DrawLike(data,channel) {
    var html = "";
    for (var i = 0;i < data.length;i ++){
        html += `<li class="inline" onclick="goDetail('${data[i].id}',${channel})">
                    <div class="recommend-cover"><img src="${data[i].image}" width="100%" height="100%"></div>
                    <p class="recommend-name">${data[i].name}</p>
                 </li>`;
    }
    $(".like-content ul").html(html);
}

function goDetail(id,channel) {
    location.href = "./detail.html?id=" + id + "&channel=" + channel;
}

function getShelfData() {
    $.ajax({
        type:'get',
        url:BASE_URl + "record/favorite?format=normal",
        success:function (data) {
            if(data.success){
                DrawShelf(data.data);
            }
        }
    });
}

function DrawShelf(data) {;
    var html = "";
    if(data){
        $(".none-tip").hide();
        for (var i = 0;i < data.length;i ++){
            html += `<li class="inline">
                    <div class="recommend-cover"><img src="${data[i].image}" width="100%" height="100%"></div>
                    <p class="recommend-name">${data[i].name}</p>
                    <i class="select-box pa" data-id="${data[i].id}" data-index="${i}"></i>
                 </li>`;
        }
    }else{
        $(".none-tip").show();
    }
    $(".shelf-content .shelf-list").html(html);
}

function deleteShelf(ids) {
    $.ajax({
        type:'delete',
        url:BASE_URl + "manage/favorite?id="+ids,
        success:function (data) {
            if(data.success){
                Toast(data.data);
                $(".cancel").hide();
                $(".manage").show();
                $(".select-all-wrapper").hide();
                getShelfData();
            }
        }
    });
}

function getTrackData(channel) {
    $.ajax({
        type:'get',
        url:BASE_URl + "record/footprint",
        success:function (data) {
            if(data.success){
                DrawTrack(data.data,channel);
            }
        }
    });
}

function DrawTrack(data,channel) {
    var html = "";
    if(data){
        $(".none-tip").hide();
        for (var i = 0;i < data.length;i ++){
            html += `<li>
                        <div class="track-book-cover inline">
                            <img src="${data[i].image}" alt="" width="100%" height="100%">
                        </div>
                        <div class="track-book-detail inline">
                            <p class="track-book-name">${data[i].name}</p>
                            <p class="track-book-status">${data[i].status}</p>
                            <div class="last-and-continue">
                            <div class="track-book-last">阅读至「<div class="last-chapter inline">${data[i].progress}</div>」</div>
                            <div class="continue-read tr" onclick="goRead('${data[i].id}','${data[i].chapter}','${channel}')">继续阅读</div>
                            </div>
                        </div>
                      </li>`;
        }
    }
    $(".track-content .track-list").html(html);
}

function goRead(id,cid,channel) {
    location.href = "./read.html?id="+id+"&cid="+cid+"&channel="+channel;
}